//
//  MPBAuthorizationCameraTools.m
//  MiaoPai
//
//  Created by jiaxuzhou on 2017/6/21.
//  Copyright © 2017年 Jeakin. All rights reserved.
//
//


#import "MPTAuthorizationCameraTools.h"


@import AVFoundation;


@interface MPTAuthorizationCameraTools ()
{
    AuthorizationHandler _completion;
}


@end


@implementation MPTAuthorizationCameraTools

/**
 注释

 @return 注释
 */
+ (instancetype)sharedInstance
{
    static MPTAuthorizationCameraTools *_instance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        _instance = [[MPTAuthorizationCameraTools alloc] init];
    });
    
    return _instance;
}

- (MPE_AuthorizationStatus)authorizationStatus
{
    if ([AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)])
    {
        AVAuthorizationStatus permission =
        [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        
        switch (permission)
        {
            case AVAuthorizationStatusAuthorized:
                return MPE_AuthorizationAuthorized;
            case AVAuthorizationStatusDenied:
            case AVAuthorizationStatusRestricted:
                return MPE_AuthorizationDenied;
            case AVAuthorizationStatusNotDetermined:
                return MPE_AuthorizationNotDetermined;
        }
    }
    else
    {
        return MPE_AuthorizationAuthorized;
    }
}

- (void)authorize:(AuthorizationHandler)completion
{
    if ([AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)])
    {
        AVAuthorizationStatus permission =
        [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        switch (permission)
        {
            case AVAuthorizationStatusAuthorized:
            {
                if (completion)
                {
                    completion(MPE_AuthorizationAuthorized, nil);
                }
            }
                break;
            case AVAuthorizationStatusDenied:
            case AVAuthorizationStatusRestricted:
            {
                if (completion)
                {
                    completion(MPE_AuthorizationDenied, nil);
                }
            }
                break;
            case AVAuthorizationStatusNotDetermined:
            {
                _completion = completion;
                [self actuallyAuthorize];
            }
                break;
        }
    }
    else
    {
        if (completion)
        {
            completion(true, nil);
        }
    }
}

- (MPE_AuthorizationType)permissionType
{
    return MPE_AuthorizationCamera;
}

- (void)actuallyAuthorize
{
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
                             completionHandler:^(BOOL granted)
    {
         if (_completion)
         {
             dispatch_async(dispatch_get_main_queue(), ^{
                 if (granted)
                 {
                     _completion(MPE_AuthorizationAuthorized, nil);
                 }
                 else
                 {
                     _completion(MPE_AuthorizationDenied, nil);
                 }
             });
         }
     }];
}


@end
